//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PopupPanelProblemFeedback extends PopupPanel implements ClickHandler {
	private static final int MAX_LENGTH = 100;
	private final TextBox textBoxFeedback = new TextBox();
	private final Button buttonSubmit = new Button("送信", this);
	private final Button buttonCancel = new Button("キャンセル", this);
	private final int problemId;
	private final boolean good;
	private final Button buttonTarget;

	public PopupPanelProblemFeedback(int problemId, boolean good, Button buttonTarget) {
		super(false, false);
		this.problemId = problemId;
		this.good = good;
		this.buttonTarget = buttonTarget;

		final VerticalPanel panelRoot = new VerticalPanel();

		{
			textBoxFeedback.setWidth("300px");
			textBoxFeedback.setMaxLength(MAX_LENGTH);
			panelRoot.add(textBoxFeedback);
		}
		{
			final HorizontalPanel panel = new HorizontalPanel();
			panel.add(buttonSubmit);
			panel.add(buttonCancel);
			panelRoot.add(panel);
		}

		setWidget(panelRoot);
	}

	private void submit() {
		final String feedback = textBoxFeedback.getText();
		if (feedback.length() == 0) {
			return;
		}

		textBoxFeedback.setEnabled(false);
		buttonSubmit.setEnabled(false);
		buttonCancel.setEnabled(false);

		final int userCode = UserData.getInstance().getUserCode();
		Service.Util.getInstance().voteToProblem(userCode, problemId, good, feedback, callbackVoteToProblem);
	}

	private final AsyncCallback<Object> callbackVoteToProblem = new AsyncCallback<Object>() {
		public void onSuccess(Object result) {
			hide();

			int counter = Integer.parseInt(buttonTarget.getText());
			++counter;
			buttonTarget.setText(Integer.toString(counter));
		}

		public void onFailure(Throwable caught) {
		}
	};

	@Override
	public void onClick(ClickEvent event) {
		final Object sender = event.getSource();
		if (sender == buttonSubmit) {
			submit();
		} else if (sender == buttonCancel) {
			hide();
		}
	}
}
